//
//  BQMineBussbackController.m
//  BeeQuick
//
//  Created by student on 2017/7/12.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQMineBussbackController.h"
#import <SVProgressHUD.h>

#define KMargin 10

@interface BQMineBussbackController ()<UITextViewDelegate>

@property(nonatomic,weak)UILabel *placeholderLable;
@property(nonatomic,weak)UILabel *showLable;
@property(nonatomic,weak)UITextView *textView;
@end

@implementation BQMineBussbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"意见反馈";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStylePlain  target:self     action:@selector(rightItemACtion)];
    self.navigationItem.rightBarButtonItem = rightItem;
    [self setupUI];
}
- (void)textViewDidChange:(UITextView *)textView{
    unsigned long num = 300 - textView.text.length;
    [self showAttributedStringWithNum:num];
}
- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.placeholderLable.hidden = YES;
}
- (void)rightItemACtion{
    
    if (self.textView.text.length == 0) {
        NSString * str = @"请输入意见,心里空空";
        [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:str];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }else if (self.textView.text.length < 5){
        NSString * str = @"请输入超过5个字啊,亲!!!";
        [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:str];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }else{
//        [SVProgressHUD show];
//        [SVProgressHUD dismissWithDelay:4];
//        NSString * str = @"发送成功";
//        [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:str];
//        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    
    
}

- (void)showAttributedStringWithNum:(unsigned long)num{
    
    NSString *strNum = [NSString stringWithFormat:@"%zd",num];
    NSString *str = [NSString stringWithFormat:@"您还可以输入%zd个字",num];
    NSMutableAttributedString *mAttStri = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = [str rangeOfString:strNum];
    [mAttStri addAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} range:range];
    self.showLable.attributedText = mAttStri.copy;
}

- (void)setupUI{
    UIView *backgroundView = [[UIView alloc]init];
    backgroundView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.bottom.leading.trailing.offset(0);
    }];
    UILabel *tipLable = [[UILabel alloc]init];
    tipLable.text = @"你的批评和建议能帮助我们更好的完善产品,请留下你的宝贵意见!";
    tipLable.font = [UIFont systemFontOfSize:13];
    tipLable.textAlignment = NSTextAlignmentLeft;
    [backgroundView addSubview:tipLable];
    [tipLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backgroundView).offset(20);
        make.leading.equalTo(backgroundView).offset(KMargin);
        make.trailing.equalTo(backgroundView).offset(-KMargin);
    }];
    UITextView *bussback = [[UITextView alloc]init];
    self.textView = bussback;
    bussback.delegate = self;
    [backgroundView addSubview:bussback];
    [bussback mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tipLable.mas_bottom).offset(20);
        make.leading.equalTo(backgroundView).offset(KMargin);
        make.trailing.equalTo(backgroundView).offset(-KMargin);
        make.height.offset(150);
    }];
    UILabel *placeholderLable = [[UILabel alloc]init];
    self.placeholderLable = placeholderLable;
    placeholderLable.text = @"请输入宝贵意见(300字以内)";
    placeholderLable.font = [UIFont systemFontOfSize:13];
    placeholderLable.textAlignment = NSTextAlignmentLeft;
    placeholderLable.textColor = [UIColor lightGrayColor];
    [bussback addSubview:placeholderLable];
    [placeholderLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(bussback);
        make.top.equalTo(bussback).offset(5);
    }];
    UILabel *label = [[UILabel alloc]init];
    self.showLable = label;
    [self showAttributedStringWithNum:300];
    [backgroundView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView);
        make.top.equalTo(bussback.mas_bottom).offset(10);
    }];
}
@end
